package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.po.BookEntity;
import com.example.demo.domain.po.BorrowEntity;
import com.example.demo.domain.po.UserEntity;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.BorrowMapper;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, BookEntity> implements BookService {
    @Autowired
    private BookMapper bookMapper;
    public List<BookEntity> getBooksByAuthor(String author){
        QueryWrapper<BookEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("author", author);
        return this.list(wrapper);
        //return bookMapper.queryBooksByAuthor(author);
    }

    public List<BookEntity> getNotBorrowedBooks()
    {
        return bookMapper.queryNotBorrowedBooks();
    }
    public List<BookEntity> getBooksByBookType(String bookType){
        QueryWrapper<BookEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("bookType", bookType);
        return this.list(wrapper);
        //return bookMapper.queryBooksByBookType(bookType);
    }
    public BookEntity getBookByBookName(String bookName) {
        QueryWrapper<BookEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("bookName", bookName);
        return this.getOne(wrapper);
    }

    public BookEntity addBook(BookEntity bookEntity){
        BookEntity book;
        this.save(bookEntity);
        book = bookEntity;
        return book;
    }

    public boolean updateBook(BookEntity bookEntity){

        return this.updateById(bookEntity);
    }
    public int deleteBook(BookEntity bookEntity){
        return this.getBaseMapper().deleteById(bookEntity);
    }
    public List<BookEntity> getBooks(){
        List<BookEntity> bList = this.list();
        return bList;
    }

    public List<BookEntity> getBooksByUserName(String username){
//        QueryWrapper<BookEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("author", author);
//        return this.list(wrapper);
        return bookMapper.queryBooksByUserName(username);
    }
}
